<template>
  <div>
    <!-- 订货单审核-->
    <TopFilter
      :list="topFilterList"
      :cols="3"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
      :labelWidth="110"
      @resetChange="resetChange"
    ></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      columnsRef="zjPickAddressSearch"
      :isSelectColumn="false"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
      </template>
    </FilterTable>

    <BaseDialog
      :visible.sync="issearch"
      :title="'经销商查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeacher   @close="dealercloseHandler" />
    </BaseDialog>

    <Drawer
      :visible.sync="detailVisible"
      :title="'订单信息'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
      width="72%"
    >
      <detailModel  :rowData="formData" @close="emitcloseHandler" />
    </Drawer>


  </div>
</template>
<script>
import  {getList,majorAddressAudit,getTypesub,getDeptList,getOfficeList,gettmFiAccountType } from '@/api/omSale/KeyCustomers/PickAddressSearch'
import detailModel from './detailModel'
import ownerSeacher from './ownerSeacher';
import { dictionary } from '@/utils/dictMixin';
import {exportExcelFile} from '@/utils'
export default {
  name:'OrderFinancialAudit',
  data(){
    this.BaseTable=null;
    return{
      topFilterList:this.createdFrom(),
      columns:this.createdTable(),
      fetchParams:{

      },
      fetchApi:getList,
      formData:{},
      issearch:false,
      detailVisible:false,
      selectList:[],
      isOpen:true,
    }
  },
  components: {
    ownerSeacher,
    detailModel,
  },
  mixins:[dictionary],
  mounted(){
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created(){
    this.getDeptList();
    this.gettmFiAccountType();
  },
  methods:{
    createdFrom(){
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          },
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入',
        },
        {
          type: 'RANGE_DATE',
          label: '提交日期',
          fieldName: 'reportDateBegin|reportDateEnd',
          placeholder: '请选择',
          initialValue:[],
          dateType:'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '事业部',
          fieldName: 'deptId',
          placeholder: '请选择',
          itemList:[],
          change:p=>{
            if(p){
              this.topFilterList.map(x=>{
                if(x.fieldName=='officeId'){
                  x.itemList=[];
                  x.initialValue='';
                }
              })
              this.getOfficeList(p)
            }else{
              this.topFilterList.map(x=>{
                if(x.fieldName=='officeId'){
                  x.itemList=[];
                  x.initialValue='';
                }
              })
            }
          }
        },
        {
          type: 'SELECT',
          label: '办事处',
          fieldName: 'officeId',
          placeholder: '请选择',
          itemList:[]
        },
        {
          type: 'RANGE_DATE',
          label: '审批日期',
          fieldName: 'checkDateBegin|checkDateEnd',
          placeholder: '请选择',
          initialValue:[],
          dateType:'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList:this.createDictList('6006'),
          change:p=>{
            if(p){
              this.topFilterList.find(x=>x.fieldName=='orderDetailType').itemList=[];
              this.topFilterList.find(x=>x.fieldName=='orderDetailType').initialValue='';
              this.getNotPage(p)
            }else{
              this.topFilterList.find(x=>x.fieldName=='orderDetailType').itemList=[];
              this.topFilterList.find(x=>x.fieldName=='orderDetailType').initialValue='';
            }
          }
        },
        {
          type: 'SELECT',
          label: '订单类型细分',
          fieldName: 'orderDetailType',
          placeholder: '请选择',
          itemList:[]
        },
        {
          type: 'INPUT',
          label: '销售订单编号',
          fieldName: 'doNo',
          placeholder: '请选择',
        },
        {
          type: 'SELECT',
          label: '资金类型',
          fieldName: 'accountType',
          placeholder: '请选择',
          itemList:[]
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength:'',
          maxRows:2
        },
        {
          type: 'SELECT',
          label: '申请状态',
          fieldName: 'auditStatus',
          placeholder: '请选择',
          itemList:this.createDictList('6012')
        },




      ]
    },
    createdTable(){
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>

                <multiuse-button  size="mini" type="text" onClick={() => this.editHandle(props.row,2)}>
                  详情
                </multiuse-button>

              </div>
            );
          }
        },
        {
          title: '事业部',
          dataIndex: 'deptName',
          sorter: true,
          filter: true,
          filterType: 'input',

        },
        {
          title: '办事处',
          dataIndex: 'officeName',
          sorter: true,
          filter: true,
          filterType: 'input',

        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订货单编号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '销售订单号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6006'),
          render: (p) => {
            return <span >{this.createDictText(p.row.orderType, '6006')}</span>;
          },
        },
        {
          title: '订单细分类型',
          dataIndex: 'orderDetailTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '修改前收车地址',
          dataIndex: 'beforeAddress',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '修改后收车地址',
          dataIndex: 'afterAddress',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '修改前联系人',
          dataIndex: 'beforeLinkMan',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '修改前联系电话',
          dataIndex: 'beforeLinkPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '修改后联系电话',
          dataIndex: 'afterLinkPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '申请状态',
          dataIndex: 'auditStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6012'),
          render: (p) => {
            return <span >{this.createDictText(p.row.auditStatus, '6012')}</span>;
          },
        },
        {
          title: '资金类型',
          dataIndex: 'accountType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editType:'select',
          editItems:[]
        },
        {
          title: 'vin',
          dataIndex: 'vin',align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },



      ]
    },
    async getDeptList(){
      let res=await getDeptList({orgType:15061005})
      let arr=res.data.map(x=>({text:x.orgName,value:x.orgId}))
      this.topFilterList.find(x=>x.fieldName=='deptId').itemList=arr;
    },
    async getOfficeList(orgid){
      let res=await getOfficeList({orgType:15061007,parentOrgId:orgid})
      let arr=res.data.map(x=>({text:x.orgName,value:x.orgId}))
      this.topFilterList.find(x=>x.fieldName=='officeId').itemList=arr;
      this.topFilterList.find(x=>x.fieldName=='officeId').initialValue='';
    },
    async getNotPage(orderType){
      let list=await getTypesub({orderType:orderType})
        this.topFilterList.map(x=>{
          if(x.fieldName=='orderDetailType'){
            x.itemList=list.data.map(x=>({text:x.subName,value:x.id}))
          }
        })
    },
    async gettmFiAccountType(){
      let res=(await gettmFiAccountType({})).data.map(x=>({text:x.accountTypeName,value:x.id}))
      this.columns.find(x=>x.dataIndex=='accountType').editItems=res;
      this.columns.find(x=>x.dataIndex=='accountType').filterItems=res;
      this.topFilterList.find(x=>x.fieldName=='accountType').itemList=res;
    },
    selectChangeHandle(row){
      if(row.length>0){
        this.selectList=row||[];
        this.isOpen=false;
      }else{
        this.selectList=[];
        this.isOpen=true;
      }
    },
    seacherFun(){
      this.issearch=true;
    },
    async addHandle(type){
      if(type==1){
        await this.$confirm('是否审核通过?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        }).then(async () => {
          let json={}
          let arr=[]
          this.selectList.map(x=>{
            arr.push(x.id)
          })
          json.deliveryOrderIds=arr
          json.auditStatus=12611002;
          let res=await majorAddressAudit(json)
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '审核通过成功',
              type: 'success'
            });
            this.isOpen=true;
            this.fetchParams=Object.assign({},this.fetchParams)
          }
        })
        .catch(()=>{})
      }else if(type==2){
        this.isreject=true;
      }
    },
    changeHandle(params){
      this.isOpen=true;
      this.fetchParams=Object.assign({},this.fetchParams,params)
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data){
      this.formData=data;
      this.detailVisible=true
    },
    emitcloseHandler(val){
      if(val){
        this.isOpen=true;
        this.fetchParams=Object.assign({},this.fetchParams)
      }
      this.detailVisible=false
    },
    rejectClose(val){
      if(val){
        this.isOpen=true;
        this.fetchParams=Object.assign({},this.fetchParams)
      }
      this.isreject=false;
    },
    dealercloseHandler(val){
      if(val){
        this.topFilterList.find(x=>x.fieldName=='dealerCode').initialValue=val.dealerCode;
      }
      this.issearch=false;
    },
    resetChange(val){
      this.topFilterList.find(x=>x.fieldName=='orderDetailType').itemList=[];
      this.topFilterList.find(x=>x.fieldName=='orderDetailType').initialValue='';
      this.fetchParams=Object.assign({},this.fetchParams)
    }
  }
}
</script>
